﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ReAnimation.MVP.ViewInterfaces;
using ReAnimation.Infrastructure.Services.Interfaces;
using ReAnimation.DAL.RepositoryInterfaces;
using Iesi.Collections;
using ReAnimation.Domain;
using System.Text.RegularExpressions;
using ReAnimation.MVP.Models.Interfaces;

namespace ReAnimation.MVP.Views
{
    public partial class MoleculesVisualizationSettingsView : BaseUserControlView, IMoleculesVisualizationSettingsView
    {
        private IApplicationSettings _applicationSettings;
        private IAtomDescriptionRepository _atomDescriptionRepository;
        private IList<AtomDescription> _descriptions;
        private IMoleculesDisplayModel _moleculesDisplayModel;

        public MoleculesVisualizationSettingsView(IApplicationSettings applicationSettings,
            IAtomDescriptionRepository atomDescriptionRepository,
            IMoleculesDisplayModel moleculesDisplayModel)
        {
            _moleculesDisplayModel = moleculesDisplayModel;
            _atomDescriptionRepository = atomDescriptionRepository;
            _applicationSettings = applicationSettings;
            InitializeComponent();
            InternalInit();
        }

        private void InternalInit()
        {
            _descriptions =  _atomDescriptionRepository.GetAll().ToList<AtomDescription>();
            atomDescriptionBindingSource.DataSource = _descriptions;
            lineWidthNumTextField.Value = _applicationSettings.LineWeight;

            linkColorPanel.BackColor = _applicationSettings.LinkColor;

            lineWidthNumTextField.ValueChanged += new EventHandler(lineWidthNumTextField_ValueChanged);
        }

        void lineWidthNumTextField_ValueChanged(object sender, EventArgs e)
        {
            _applicationSettings.LineWeight = lineWidthNumTextField.Value;
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                _atomDescriptionRepository.UpdateAtomDescription(_descriptions[e.RowIndex]);
                _moleculesDisplayModel.RadiusAndColorWasUpdated();
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            
                e.CellStyle.BackColor = _descriptions[e.RowIndex].Color;
        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                String s = e.FormattedValue as String;
                Regex r = new Regex("^[0-9]+.?[0-9]*$");
                e.Cancel = !r.IsMatch(s);
            }
        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            
        }

        private void changeColorBtn_Load(object sender, EventArgs e)
        {
            
        }

        private void changeColorBtn_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count != 0)
            {
                var enumerator = dataGridView1.SelectedRows.GetEnumerator();
                enumerator.MoveNext();
                DataGridViewRow row = enumerator.Current as DataGridViewRow;

                var desc = _descriptions[row.Index];
                colorDialog1.Color = desc.Color;
                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    desc.Color = colorDialog1.Color;
                    _atomDescriptionRepository.UpdateAtomDescription(desc);
                    row.DefaultCellStyle.BackColor = desc.Color;

                    _moleculesDisplayModel.RadiusAndColorWasUpdated();
                }
            }
        }

        private void applyBtn_Click(object sender, EventArgs e)
        {
            
        }

        private void linkColorPanel_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                _applicationSettings.LinkColor = colorDialog1.Color;
                linkColorPanel.BackColor = colorDialog1.Color;
            }
        }

        private void applyBtn_Load(object sender, EventArgs e)
        {

        }
    }
}
